#include"Heap.h"




void HeapInit(HP* head)
{
	assert(head);
	head->a =(HPdatanode*) malloc(sizeof(HPdatanode));
	//head->a = NULL;
	head->capacity = 1;
	head->size = 0;
}
void HeapDestory(HP* head)
{
	assert(head);
	free(head->a);
	head->a = NULL;
	head->capacity = 0;
	head->size = 0;
}
void HangeHeap(HP* head, HPdatanode chile)
{
	assert(head);
	HPdatanode elder = (chile - 1) / 2;
	while (chile > 0)
	{
		if (head->a[chile]<head->a[elder])
		{
			HPdatanode tmp = head->a[chile];
			head->a[chile] = head->a[elder];
			head->a[elder] = tmp;
			chile = elder;
			elder = (chile - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

void HeapPush(HP* head, HPdatanode x)
{
	assert(head);
	if (head->size == head->capacity)
	{ 
		HPdatanode* tmp =(HPdatanode*) realloc(head->a, sizeof(HPdatanode) * head->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		head->a = tmp;
		head->capacity = head->capacity*2;
	}
	head->a[head->size] = x;
	head->size++;
	HangeHeap(head, head->size - 1);
	/*if (head->size == 1)
	{
		return;
	}*/
	

}